<template>
	<view>
		<navbar title="修改信息">
		</navbar>
		<view style="margin: 10px;padding: 10px;background-color: white;border-radius: 10px;">
			<!-- 更换头像 -->
			<view style="display: flex;align-items: center;">
				<view style="flex: 1;display: flex;align-items: center;justify-content:flex-end;">
					<uni-file-picker :value="[{
						url:userInfo.avatar
					}]" :auto-upload="false" limit="1" :del-icon="false" disable-preview :imageStyles="imageStyles"
						file-mediatype="image" @select="uploadicon">选择</uni-file-picker>
					<p>头像</p>
				</view>
			</view>
			<!-- 分割线 -->
			<view style="height: 1px;background-color: #f6f6f6;margin-top: 5px;" />
			<!-- 个人信息列表 -->
			<view v-for="(item,index) in list" @click="onclick(index)">
				<view v-if="item.show" style="display: flex;align-items: center;height: 50px;">
					<p>{{item.title}}</p>
					<view style="flex: 1;display: flex;align-items: center;justify-content:flex-end;">
						<p>{{item.content}}</p>
						<uni-icons v-if="item.click" type="right" size="25"></uni-icons>
					</view>
				</view>
				<view v-if="item.show" style="height: 1px;background-color: #f6f6f6;">
				</view>
			</view>
			<view @click="copy(userInfo._id)" style="display: flex;align-items: center;height: 50px;">
				<p>Key</p>
				<view style="flex: 1;display: flex;align-items: center;justify-content:flex-end;">
					<p>{{userInfo._id}}</p>
				</view>
			</view>
			<view style="height: 1px;background-color: #f6f6f6;">
			</view>
		</view>
		<button style="margin: 10px;" type="warn" @click="removelogin">退出登录</button>
		<p style="margin-left: 10px;font-size: 14px;">tip:联系方式用于分享文件时展示给接收者</p>
		<view>
			<!-- 修改用户信息 -->
			<u-modal @cancel="setuserinfo=false" showCancelButton @close="setuserinfo=false" closeOnClickOverlay
				:show="setuserinfo" :title="inputdata.title" @confirm="infoConfirm">
				<u--input placeholder="请输入修改内容" border="surround" v-model="inputdata.content"></u--input>
			</u-modal>
		</view>
		<view>
			<!-- 修改用户密码 -->
			<u-popup @close="setpassword=false" closeOnClickOverlay :show="setpassword" mode="bottom" round="10">
				<view style="padding: 20px;">
					<h3 style="text-align: left;">修改密码</h3>
					<u--input placeholder="请输入旧密码" style="margin:10px 0" border="surround"
						v-model="password_old"></u--input>
					<u--input placeholder="请输入新密码" style="margin: 10px 0" border="surround"
						v-model="password_new"></u--input>
					<u-button type="primary" @click="passwordConfirm" text="修改"></u-button>
				</view>
			</u-popup>
		</view>
		<view>
			<u-modal @cancel="pointout=false" showCancelButton @close="pointout=false" closeOnClickOverlay
				:show="pointout" title="温馨提示" content="你正在执行退出账户操作,此操作不可逆" @confirm="pointoutConfirm">
			</u-modal>
		</view>
	</view>
</template>

<script>
	import store from '@/store/index.js';
	import ase from '@/common/ar-aes/ar-aes.js'
	const uniIdCo = uniCloud.importObject("uni-id-co")
	export default {
		data() {
			return {
				setpassword: false,
				pointout: false,
				setuserinfo: false,
				imageStyles: {
					width: 64,
					height: 64,
					border: {
						radius: '50%'
					}
				},
				password_old: null,
				password_new: null,
				inputdata: {
					content: null,
					type: null,
					title: null,
					index: null
				},
				list: [{
					title: "用户昵称",
					content: null,
					type: "nickname",
					click: true,
					show: true
				}, {
					title: "用户名",
					content: null,
					type: "username",
					click: false,
					show: true
				}, {
					title: "邮箱",
					content: null,
					type: "email",
					click: false,
					show: false
				}, {
					title: "UID",
					content: null,
					type: "uid",
					click: false,
					show: false
				}, {
					title: "联系方式",
					content: null,
					type: "tel",
					click: true,
					show: true
				}, {
					title: "用户密码",
					content: null,
					type: "password",
					click: true,
					show: true
				}, {
					title: "注册时间",
					content: null,
					type: "register_date",
					click: false,
					show: true
				}],
				userInfo: store.state.userInfo,
			};
		},
		methods: {
			onclick(i) {
				if (i == 0 || i == 4) {
					this.setinfo(i)
				} else if (i == 5) {
					this.setpassword = true
				} else if (i == 1 && this.list[i].click) {
					uni.navigateTo({
						url: '/pages/username/username'
					});
				} else {
					this.copy(this.list[i].content)
				}
			},
			async uploadicon(e) {
				uni.showLoading({
					title: '图片上传中'
				});
				let that = this
				uni.uploadFile({
					url: "https://thumbsnap.com/api/upload",
					filePath: e.tempFilePaths[0],
					name: 'media',
					formData: {
						'key': "0004584d45c9641621a1ba89160b46d9"
					},
					success(res) {
						uniCloud.callFunction({
							name: 'setuserinfo',
							data: {
								data: ase.encrypt(JSON.stringify({
									form: {
										avatar: JSON.parse(res.data).data.thumb
									}
								}), 'yclycl1314520520', '9740022097400220')
							},
							success() {
								uni.hideLoading();
								that.message('修改成功')
								that.userInfo.avatar = JSON.parse(res.data).data.thumb
								store.commit('setUserInfo', that.userInfo)
							}
						});
					}
				})
			},
			setinfo(i) {
				this.inputdata.type = this.list[i].type;
				this.inputdata.index = i;
				this.inputdata.title = '修改' + this.list[i].title;
				this.inputdata.content = this.list[i].content;
				this.setuserinfo = true
			},
			removelogin() {
				this.pointout = true
			},
			passwordConfirm(val) {
				if (this.password_old == null || this.password_new == null) {
					this.message('参数不完整', 'error')
					return false
				}
				uniIdCo.updatePwd({
					oldPassword: this.password_old,
					newPassword: this.password_new
				})
				this.setpassword = false
			},
			infoConfirm() {
				var form;
				if (this.inputdata.type == 'nickname') {
					form = {
						nickname: this.inputdata.content
					}
					this.userInfo.nickname = this.inputdata.content
					this.list[0].content = this.inputdata.content
				} else {
					form = {
						tel: this.inputdata.content
					}
					this.userInfo.tel = this.inputdata.content
					this.list[4].content = this.inputdata.content
				}
				var that = this;
				uniCloud.callFunction({
					name: 'setuserinfo',
					data: {
						data: ase.encrypt(JSON.stringify({
							form: form
						}), 'yclycl1314520520', '9740022097400220')
					},
					success(res) {
						that.message('修改成功')
						that.setuserinfo = false
					}
				});
				store.commit('setUserInfo', this.userInfo)
			},
			pointoutConfirm() {
				uniIdCo.logout().then(e => {
					uni.removeStorageSync('uni_id_token')
					uni.removeStorageSync('uni_id_token_expired')
					uni.reLaunch({
						url: '../login/login'
					});
				})
			},
			getuserinfo() {
				this.list[4].content = this.userInfo.tel;
				this.list[0].content = this.userInfo.nickname;
				if (this.userInfo.username == null) {
					this.list[1].content = "未绑定";
					this.list[1].click = true;
				} else {
					this.list[1].content = this.userInfo.username
				}
				if (this.userInfo.email != null) {
					this.list[2].show = true
					this.list[2].content = this.userInfo.email;
				}
				if (this.userInfo.uid != null) {
					this.list[3].show = true;
					this.list[3].content = this.userInfo.uid;
				}
				if (this.userInfo.email == null && this.userInfo.username == null) {
					this.list[5].show = false;
					this.list[5].content = this.userInfo.uid;
				}
				var time = new Date(this.userInfo.register_date)
				var register_date = time.getFullYear().toString() + '年' + (time.getMonth() + 1)
					.toString() + '月' + time.getDate().toString() + '日 ' + time.getHours().toString() +
					'时' +
					time.getMinutes().toString() + '分';
				this.list[6].content = register_date;
			}
		},
		onLoad() {
			this.getuserinfo();
		}
	}
</script>

<style lang="less">
	.example-body {
		padding: 10px;
		padding-top: 0;
	}

	.custom-image-box {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		justify-content: space-between;
		align-items: center;
	}

	.text {
		font-size: 14px;
		color: #333;
	}
</style>